//
//  AppDelegate.m
//  Listening
//
//  Created by Meniny on 6/3/12.
//  Copyright (c) 2012 Meniny. All rights reserved.
//

#import "AppDelegate.h"
#import <ServiceManagement/ServiceManagement.h>
#import "MXHotKeyManager.h"
#import "NSUserNotification+Quick.h"
#import "MXLauncher.h"

@interface AppDelegate () <NSUserNotificationCenterDelegate>

@property (nonatomic) NSRunningApplication *iChat;

@property (nonatomic, retain) NSStatusItem *statusItem;
@property (assign) IBOutlet NSMenuItem *autoLaunchItem;
@end

@implementation AppDelegate

@synthesize iChat;

@synthesize statusItem;
@synthesize statusMenu;

- (void)dealloc {
    [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
    
    self.iChat = nil;
    
    self.statusItem = nil;
    self.statusMenu = nil;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [[MXHotKeyManager defaultManager] foo];
}

- (void)awakeFromNib {
//    NSLau
//    self.iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
    
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    NSImage *image = [NSImage imageNamed:@"StatusIcon"];
    image.template = YES;
    self.statusItem.image = image;
    self.statusItem.menu = self.statusMenu;
    self.statusItem.highlightMode = YES;
    self.statusItem.toolTip = @"iChat Shortcut";
    
    self.autoLaunchItem.state = [[NSUserDefaults standardUserDefaults] integerForKey:@"cn.meniny.iChatShortcut.LaunchAtLogin"];
}



- (IBAction)launchOption:(NSMenuItem *)sender {
    NSInteger newState = 1 - sender.state;
    if (SMLoginItemSetEnabled((__bridge CFStringRef)@"cn.meniny.iChatShortcutHelper", (BOOL)newState)) {
        sender.state = newState;
        [[NSUserDefaults standardUserDefaults] setInteger:sender.state forKey:@"cn.meniny.iChatShortcut.LaunchAtLogin"];
    } else {
        [NSUserNotification notification:NSLocalizedString(@"OptionChangeFailed", nil)
                   message:NSLocalizedString(@"LaunchAtLogin", nil)
                     image:nil
                     sound:YES
                identifier:nil];
    }
}

- (IBAction)launchiChat:(NSMenuItem *)sender {
    [MXLauncher launchiChat];
}

- (NSString *)versionInfo {
    NSString *version = [self version];
    return version ? [NSString stringWithFormat:@"%@ v%@", [self appName], version] : [self appName];
}

- (NSString *)appName {
    return NSLocalizedString(@"AppName", nil);
}

- (NSString *)version {
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    return [NSString stringWithFormat:@"%@ (%@)", version, build];
}

@end
